package com.linkage.order.service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.ibatis.annotations.Param;
import org.omg.CORBA.PRIVATE_MEMBER;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter;
import org.springframework.stereotype.Service;

import com.linkage.book.pojo.Book;
import com.linkage.book.repository.BookRepository;
import com.linkage.order.pojo.Order;
import com.linkage.order.repository.OrderRepository;
import com.linkage.user.pojo.User;
import com.linkage.user.repository.UserRepository;
/**
 * 
 * @author XiaMingYu
 *
 */
@Service
public class OrderService {
	@Autowired
	private OrderRepository orderRepository;
	private UserRepository userRepository;
	private BookRepository bookRepository;
	private Order order;
	private User user;
	private Book book;
	
	public List<Order> queryAll(){
     return this.orderRepository.queryAll();		
	}
	
	public List<Order> query(String orderId,Date orderTime,double total,int status){
	return this.orderRepository.query(orderId,orderTime,total,status);
	}
	
	public List<Order> queryItem(String orderId,Date orderTime,double total,int status,double subTotal){
		return this.orderRepository.queryItem(orderId,orderTime,total,status,subTotal);
	}
	
	public void saveOrder(Order order){
		order.setorderId(UUID.randomUUID().toString());
	}
	
	public void deleteOrder(String orderId){
		this.orderRepository.deleteOrder(orderId);
	}
	
	public Order findOrderById(String orderId){
		return this.orderRepository.findOrderById(orderId);
	}
	
	public double calTotal(String bookId ,int bookIdQuantity ){
		book = this.bookRepository.findBookById(bookId);
	    double total = book.getPrice()*bookIdQuantity ;
		return total;
	}
	
	public void payOrder(String orderId,int userId){
		user = this.userRepository.findUserById(userId);
		order =this.orderRepository.findOrderById(orderId);
		if(user.getMoney()<=0){
			return ;
		}
		else if(order.getStatus()==1) {
			user.setMoney(user.getMoney()-order.getTotal());
			order.setStatus(2);
			return;
		}
	}
	
	public void deliver(String orderId){
		order = this.orderRepository.findOrderById(orderId);
		if(order.getStatus()!=2){
			return;
		}
		else 
		order.setStatus(3);
		return;
	}
	
	public void confirm(String orderId){
		order =this.orderRepository.findOrderById(orderId);
		if(order.getStatus()!=3){
			return;
		}
		else {
			order.setStatus(4);
			return;
		}	
	}
}



